<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
</head>

<body>
    <div id="app">
        <button @click='orderByAge(0)'>默认</button>
        <button @click='orderByAge(1)'>升序</button>
        <button @click='orderByAge(2)'>降序</button>
        <hr/>
        <label>按姓名查找&nbsp;</label>
        <input type="text" placeholder="请输入搜索姓名" v-focus v-model='findName'/>
        <ul>
            <li v-for="(person,index) in filterName">
                {{index+1}}.姓名：{{person.name}}--年龄:{{person.age}}--性别：{{person.sex}}--qq:{{person.qq}}--联系方式:{{person.phone}}
            </li>
        </ul>
    </div>
    <script>
        Vue.directive('focus',{
            inserted:function(el){
                el.focus()
            }
        })

        var vm=new Vue({
           el:'#app',
           data:{
               findName:'',
               persons:[
                   {name:'王凯',age:24,sex:'男',qq:'213564864613',phnoe:'1331313215615'},
                   {name:'张俊',age:24,sex:'男',qq:'213564864613',phnoe:'1331313215615'},
                   {name:'王艺璇',age:22,sex:'女',qq:'213564864613',phnoe:'1331313215615'},
                   {name:'王雨馨',age:20,sex:'男',qq:'213564864613',phnoe:'1331313215615'},
                   {name:'肖战',age:23,sex:'男',qq:'213564864613',phnoe:'1331313215615'},
                   {name:'薛之谦',age:44,sex:'女',qq:'213564864613',phnoe:'1331313215615'},
                   {name:'胡歌',age:14,sex:'男',qq:'213564864613',phnoe:'1331313215615'},
                   {name:'霍建华',age:54,sex:'男',qq:'213564864613',phnoe:'1331313215615'}
               ],
               orderType:0
           },
           computed:{
               filterName(){
                   let {findName,persons,orderType}=this;
                   let arr=[...persons];
                   if(findName.trim()){
                       arr=persons.filter(p=>p.name.indexOf(findName) !=-1)
                   }

                   if(orderType){
                       arr.sort((p1,p2)=>{
                           if(orderType===1){
                               return p2.age-p1.age
                           }
                           else{
                               return p1.age-p2.age
                           }
                       });
                   }
                   return arr;
               }
           },
           methods:{
               orderByAge(orderType){
                   this.orderType=orderType;
               }
           }
        });
    </script>
</body>

</html>